package com.jiemengshi.server.service;

import com.jiemengshi.server.mapper.DreamRecordMapper;
import com.jiemengshi.server.mapper.DreamReplyMapper;
import com.jiemengshi.server.model.DreamRecord;
import com.jiemengshi.server.model.DreamRecordUser;
import com.jiemengshi.server.model.DreamReply;
import com.jiemengshi.server.util.DateUtil;
import org.apache.http.HttpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * Created by MingQi on 2017/10/14.
 */
@Service
public class AdminDreamService {
    @Autowired
    private DreamReplyMapper dreamReplyMapper;

    @Autowired
    private DreamRecordMapper dreamRecordMapper;

    @Autowired
    private WechatService wechatService;
    
    public List<DreamReply> getDreamReplyByRecordId(int recordId) {
        return dreamReplyMapper.selectByRecordId(recordId);
    }

    public boolean addDreamReply(DreamReply dreamReply, String appName) throws IOException, HttpException {
        dreamReply.setId(null);
        dreamReply.setCreateTime(DateUtil.getCurrentTime());

        boolean success = dreamReplyMapper.insertSelective(dreamReply) > 0;
        if (success) {
            dreamRecordMapper.updateNewReply(dreamReply.getDreamRecordId(), 1);
            dreamRecordMapper.updateUserReply(dreamReply.getDreamRecordId(), 0);
            dreamRecordMapper.updateNeedHelp(dreamReply.getDreamRecordId(), 0);
            
            DreamRecord record = dreamRecordMapper.selectByPrimaryKey(dreamReply.getDreamRecordId());
            wechatService.sendDreamReplyInformTemplateMsg(record.getWechatUserId(), appName, dreamReply.getContent());
        }
        
        return success;
    }

    public boolean deleteDreamReplyById(int id) {
        return dreamReplyMapper.deleteByPrimaryKey(id) > 0;
    }

    public List<DreamRecordUser> listDreamRecordByTime(int start, int limit) {
        List<DreamRecordUser> recordUsers = dreamRecordMapper.listByTime(start * limit, limit);
        for (DreamRecordUser recordUser: recordUsers) {
            recordUser.setDreamReplies(getDreamReplyByRecordId(recordUser.getId()));
        }
        return recordUsers;
    }

    public int countDreamRecordByTime() {
        return dreamRecordMapper.countByTime();
    }
    
    public List<DreamRecordUser> listDreamRecordByUser(int start, int limit) {
        List<DreamRecordUser> recordUsers = dreamRecordMapper.listByUser(start * limit, limit);
        for (DreamRecordUser recordUser: recordUsers) {
            recordUser.setDreamReplies(getDreamReplyByRecordId(recordUser.getId()));
        }
        return recordUsers;
    }

    public int countDreamRecordByUser() {
        return dreamRecordMapper.countByUser();
    }

    public List<DreamRecordUser> searchDreamRecordByUserName(String nickname, int start, int limit) {
        List<DreamRecordUser> recordUsers = dreamRecordMapper.searchByNickname(nickname, start * limit, limit);
        for (DreamRecordUser recordUser: recordUsers) {
            recordUser.setDreamReplies(getDreamReplyByRecordId(recordUser.getId()));
        }
        return recordUsers;
    }

    public int countDreamRecordByUserId(int wechatUserId) {
        return dreamRecordMapper.countByUserId(wechatUserId);
    }
    
    public int countDreamRecordByNickname(String nickname) {
        return dreamRecordMapper.countByNickname(nickname);
    }
}
